package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.User;
import com.itheima.health.service.MemberService;
import com.itheima.health.vo.OrderSubmitParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 会员SERVICE实现类
 */
@Service
@Slf4j
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    @Override
    public Member smsLogin(String telephone) {

        // 判断用户登录的手机号是否是新用户
        Member member = memberDao.selectByUserId(telephone);
        if (member == null) {

            // 新用户,添加进数据库
            memberDao.add(telephone);

            // 添加完根据手机号查询一下并返回
            member = memberDao.selectByUserId(telephone);

            // 返回查询结果
            return member;
        }

        // 不是新用户直接返回
        return member;
    }

    /**
     * 预约校验 -- 是否是新用户
     * @param orderSubmitParam 预约表单
     */
    @Override
    public void submit(OrderSubmitParam orderSubmitParam) {

        // 判断用户登录的手机号是否是新用户
        Member member = memberDao.selectByUserId(orderSubmitParam.getTelephone());
        if (member == null) {

            // 新用户,吧信息存入进去
            memberDao.submit(orderSubmitParam);
        }

        // 其他信息是否录入
        log.info("-------------------");
        int count = memberDao.seleceByIdCard(orderSubmitParam.getIdCard());
        log.info("************************"+count);
        if (count == 0) {

            // 吧信息补全 -- 默认name也没写,一次性补全
            memberDao.updataIdCard(orderSubmitParam);
        }

    }
    /**
     * 根据手机号查询用户id
     * @param telephone 用户提交的预约表单里面的手机号
     * @return 返回查询结果
     */
    @Override
    public int selectByPhone(String telephone) {

        return memberDao.selectByPhone(telephone);
    }
}
